//
//  MainTableView.m
//  Test
//
//  Created by apple on 2018/11/9.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "MainTableView.h"
#import "SubTableView.h"

@interface MainTableView ()<UITableViewDelegate,UITableViewDataSource>

/** 下一级界面 */
@property (nonatomic, strong) SubTableView *subVC;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MainTableView
/** 屏幕宽 */
#define G_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
/** 屏幕高 */
#define G_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define ROWNUM 20
#define ROWHEIGHT 50
#define NAVIHEIGHT 64

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.view addSubview:self.tableView];
//    [self.view addSubview:self.subVC.view];
//    self.subVC.view.frame = CGRectMake(0, G_SCREEN_HEIGHT, G_SCREEN_WIDTH, G_SCREEN_HEIGHT);
    self.view.backgroundColor = [UIColor whiteColor];
    [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return ROWNUM;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ROWHEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%d",(int)indexPath.row];

    return cell;
}
#pragma mark - ScrollViewDelegate

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    CGFloat edge = self.tableView.contentOffset.y  +64;
    CGFloat tableViewHeight = self.tableView.bounds.size.height;
    /** TableView内容的高度 Cell高度*Row + ，也可用高度缓存直接取  */
    CGFloat maxHeight =  (ROWHEIGHT * ROWNUM + NAVIHEIGHT) - tableViewHeight;
    BOOL isBottom = (edge >= maxHeight);
    NSLog(@"tableViewHeight: %f edge: %f isBottom: %d",tableViewHeight,edge, isBottom);
    if (isBottom) {
//        //弹出动画
//        [UIView animateWithDuration:1 animations:^{
//            self.subVC.view.frame = CGRectMake(0, 0, G_SCREEN_WIDTH, G_SCREEN_HEIGHT);
//            self.tableView.frame = CGRectMake(0, -G_SCREEN_HEIGHT, G_SCREEN_WIDTH, G_SCREEN_HEIGHT);
//            self.subVC.view.hidden = NO;
//        } completion:^(BOOL finished) {
//            self.tableView.hidden = YES;
//        }];
        [self pushFromBottom];
    }
}

- (void) pushFromBottom {
    CATransition *transitiion = [CATransition animation];
    transitiion.type = kCATransitionMoveIn;
    transitiion.subtype = kCATransitionFromTop;
    [self.navigationController.view.layer addAnimation:transitiion forKey:kCATransition];
    [self.navigationController pushViewController:self.subVC animated:NO];
}

#pragma mark - getter

- (SubTableView *)subVC {
    if (!_subVC) {
        SubTableView *subVC = [[SubTableView alloc] init];
        [self addChildViewController:subVC];
        _subVC = subVC;
    }
    return _subVC;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, G_SCREEN_WIDTH, G_SCREEN_HEIGHT) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        _tableView = tableView;
    }
    return _tableView;
}

@end
